<?php
abstract class APPCore_Admin_Controller_Action extends Zend_Controller_Action
{

    protected $cache = null;

    public function init()
    {
        /**
         * Check Login
         */
        $auth = Zend_Auth::getInstance()->getStorage('BO');
        $data = $auth->read();
        if (empty($data)) {
            if (!in_array($this->_request->getActionName(), array('login', 'get-salt'))) {
                $urlOptions = array('module' => 'admin', 'controller' => 'auth', 'action' => 'login');
                $this->_helper->redirector->gotoRoute($urlOptions);
            }
        } else {
            $this->view->assign('userProfile', $data);
        }

        $router = $this->getFrontController()->getRouter();
        $router->addRoute('requestVars', new APPCore_Controller_Router_Route_RequestVars());

        /**
         * Site Config
         */
        $config = Zend_Registry::get('configuration');
        date_default_timezone_set($config->datetime->timezone);

        // Set doctype and charset
        $this->view->setEncoding('UTF-8');
        $this->view->doctype('XHTML1_STRICT');

        // Load meta tags
        $this->view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=' . $config->charset);
        $this->view->headMeta()->appendHttpEquiv('Content-Language', 'en-US');
        $this->view->headMeta()->appendName('keywords', $config->meta->keywords);
        $this->view->headMeta()->appendName('author', $config->meta->author);
        $this->view->headMeta()->appendName('description', $config->meta->description);

        // Setting up title
        $this->view->headTitle()->setSeparator(' - '); // setting a separator string for segments:
        $this->view->headTitle($config->title);
        
        // Auto load scripts to viewers!!
        $this->view->addScriptPath(APP_PATH . DS . "modules" . DS . $this->getRequest()->getModuleName() . DS . 'layouts');
        
        // load block
        $this->loadBlock();
    }

    /**
     * Pre-dispatch routines
     *
     * Called before action method. If using class with
     * {@link Zend_Controller_Front}, it may modify the
     * {@link $_request Request object} and reset its dispatched flag in order
     * to skip processing the current action.
     *
     * @return void
     *
     * Override from Zend Controller Action
     * @implements :
     */
    public function preDispatch()
    {
        $config = Zend_Registry::get('configuration');
        $this->view->assign('config', $config);

        $frontendOptions = array('lifetime' => null, 'automatic_serialization' => true);
        $backendOptions = array('cache_dir' => $config->cache_dir); // Directory where to put the cache files
        $this->cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
        Zend_Registry::set('cache', $this->cache);
        $this->view->assign('cache', $this->cache);
    }

    /**
     * Post-dispatch routines
     *
     * Called after action method execution. If using class with
     * {@link Zend_Controller_Front}, it may modify the
     * {@link $_request Request object} and reset its dispatched flag in order
     * to process an additional action.
     *
     * Common usages for postDispatch() include rendering content in a sitewide
     * template, link url correction, setting headers, etc.
     *
     * @return void
     *
     *
     * Override from Zend Controller Action
     * @implements :
     */
    public function postDispatch()
    { // to do ...
    }

    /**
     * @todo load all block
     * @return void
     */
    private function loadBlock()
    {
        $blocksConfig = new Zend_Config_Ini(APP_PATH . DS . 'configs' . DS . 'blocks.ini');
        $blocks = $blocksConfig->toArray();
        foreach ($blocks as $blockName => $options) {
            if ($options ['display'] == 'all') {
                $this->_helper->block->add($blockName);
            } elseif ($options ['display'] != 'none') {
                $modules = explode(',', $options ['display']);
                for ($i = 0; $i < count($modules); $i++) {
                    if ($this->_request->getModuleName() == $modules [$i]) {
                        $this->_helper->block->add($blockName);
                        break;
                    }
                }
            }
        }
    }

}